package com.FengChaoSystem.untils;


import com.FengChaoSystem.pojo.New;
import com.FengChaoSystem.service.NewImpl;
import com.FengChaoSystem.service.NewService.NewService;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 用于发送qq邮箱的验证码的工具类
 */
public class EmailUtil {
    private static String fromEmail = "2970025314@qq.com";  // 发件人账号
    private static String fromEmailPw = "dxgnycyarlcldfdb";  // 发件人的生成的授权码
    private static String myEmailSMTPHost = "smtp.qq.com";  // 发件邮箱服务器
    private static Properties props;  // 用于参数配置
    private static Session session;  // 用于创建会话对象
    private String vCode;
    public static EmailUtil instance = new EmailUtil();
    private NewImpl service=new NewService();

    public static String vericodeHtml = "<!DOCTYPE html>\n" +
            "<html lang=\"en\">\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\">\n" +
            "    <title> 邮箱验证码</title>\n" +
            "    <style>\n" +
            "\n" +
            "        .main {\n" +
            "            margin: 10px auto;\n" +
            "            width: 520px;\n" +
            "\n" +
            "            border-top: 4px solid #9373EE;\n" +
            "            padding: 24px 24px 40px;\n" +
            "            border-radius:0 0 8px 8px;\n" +
            "            box-shadow: 0px 0px 1px;\n" +
            "        }\n" +
            "\n" +
            "        .title {\n" +
            "            margin: 80px auto 32px;\n" +
            "            font-size: 32px;\n" +
            "            font-weight: 600;\n" +
            "            line-height: 45px;\n" +
            "            letter-spacing: 0px;\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "        .note {\n" +
            "            margin: 0 auto;\n" +
            "            font-size: 18px;\n" +
            "            line-height: 1.4;\n" +
            "            left: 0px;\n" +
            "            top: 77px;\n" +
            "            font-weight: 400;\n" +
            "        }\n" +
            "\n" +
            "        .code {\n" +
            "            padding: 16px;\n" +
            "            text-align: center;\n" +
            "            background: rgba(147, 115, 238, 0.04);\n" +
            "            border-radius: 4px;\n" +
            "            font-weight: 600;\n" +
            "            font-size: 24px;\n" +
            "            line-height: 140%;\n" +
            "            color: #9373EE;\n" +
            "            margin: 24px 0;\n" +
            "            letter-spacing: 1px;\n" +
            "        }\n" +
            "\n" +
            "        .claim ul {\n" +
            "            margin-top: 34px;\n" +
            "            margin-bottom: 40px;\n" +
            "            font-size: 13px;\n" +
            "            line-height: 1.6;\n" +
            "            color: #5c5c5c;\n" +
            "            padding: 25px 0;\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "        .claim ul li {\n" +
            "            color: rgba(24, 24, 25, 0.42);\n" +
            "            line-height: 30px;\n" +
            "        }\n" +
            "\n" +
            "        .footer {\n" +
            "            font-size: 13px;\n" +
            "            line-height: 1.6;\n" +
            "            color: #5c5c5c;\n" +
            "            padding: 25px 0\n" +
            "        }\n" +
            "        .title,.note,.claim,.footer {\n" +
            "            text-align: center;\n" +
            "        }\n" +
            "    </style>\n" +
            "</head>\n" +
            "<body>\n" +
            "<div class=\"main\">\n" +
            "    <div class=\"title\"> 邮箱账号验证码</div>\n" +
            "    <div class=\"note\">你正在进行邮箱验证操作，验证码为:</div>\n" +
            "    <div class=\"code\" :data=\"123456\">1EM456</div>\n" +
            "\n" +
            "    <div class=\"claim\">\n" +
            "        <ul style=\"list-style: none;\">\n" +
            "            <li style=\"list-style: none;\">此验证码 15 分钟内有效</li>\n" +
            "            <li style=\"list-style: none;\">如非本人操作</li>\n" +
            "            <li style=\"list-style: none;\">转给他人将导致账号被盗和个人信息泄漏，谨防诈骗</li>\n" +
            "        </ul>\n" +
            "    </div>\n" +
            "\n" +
            "    <div class=\"footer\">\n" +
            "    </div>\n" +
            "</div>\n" +
            "</body>\n" +
            "</html>";

private String overTimeEmail="<!DOCTYPE html>\n" +
        "<html lang=\"en\">\n" +
        "<head>\n" +
        "    <meta charset=\"UTF-8\">\n" +
        "    <title> 用户您的快递滞留时间已超过18小时！将收取费用</title>\n" +
        "    <style>\n" +
        "\n" +
        "        .main {\n" +
        "            margin: 10px auto;\n" +
        "            width: 520px;\n" +
        "\n" +
        "            border-top: 4px solid #9373EE;\n" +
        "            padding: 24px 24px 40px;\n" +
        "            border-radius:0 0 8px 8px;\n" +
        "            box-shadow: 0px 0px 1px;\n" +
        "        }\n" +
        "\n" +
        "        .title {\n" +
        "            margin: 80px auto 32px;\n" +
        "            font-size: 32px;\n" +
        "            font-weight: 600;\n" +
        "            line-height: 45px;\n" +
        "            letter-spacing: 0px;\n" +
        "\n" +
        "        }\n" +
        "\n" +
        "        .note {\n" +
        "            margin: 0 auto;\n" +
        "            font-size: 18px;\n" +
        "            line-height: 1.4;\n" +
        "            left: 0px;\n" +
        "            top: 77px;\n" +
        "            font-weight: 400;\n" +
        "        }\n" +
        "\n" +
        "        .code {\n" +
        "            padding: 16px;\n" +
        "            text-align: center;\n" +
        "            background: rgba(147, 115, 238, 0.04);\n" +
        "            border-radius: 4px;\n" +
        "            font-weight: 600;\n" +
        "            font-size: 24px;\n" +
        "            line-height: 140%;\n" +
        "            color: #9373EE;\n" +
        "            margin: 24px 0;\n" +
        "            letter-spacing: 1px;\n" +
        "        }\n" +
        "\n" +
        "        .claim ul {\n" +
        "            margin-top: 34px;\n" +
        "            margin-bottom: 40px;\n" +
        "            font-size: 13px;\n" +
        "            line-height: 1.6;\n" +
        "            color: #5c5c5c;\n" +
        "            padding: 25px 0;\n" +
        "\n" +
        "        }\n" +
        "\n" +
        "        .claim ul li {\n" +
        "            color: rgba(24, 24, 25, 0.42);\n" +
        "            line-height: 30px;\n" +
        "        }\n" +
        "\n" +
        "        .footer {\n" +
        "            font-size: 13px;\n" +
        "            line-height: 1.6;\n" +
        "            color: #5c5c5c;\n" +
        "            padding: 25px 0\n" +
        "        }\n" +
        "        .title,.note,.claim,.footer {\n" +
        "            text-align: center;\n" +
        "        }\n" +
        "    </style>\n" +
        "</head>\n" +
        "<body>\n" +
        "<div class=\"main\">\n" +
        "    <div class=\"title\"> 以下为您滞留的快递订单号</div>\n" +
        "    <div class=\"note\">订单号为:</div>\n" +
        "    <div class=\"code\" :data=\"123456\">1EM456</div>\n" +
        "\n" +
        "\n" +
        "    <div class=\"footer\">\n" +
        "    </div>\n" +
        "</div>\n" +
        "</body>\n" +
        "</html>";


    /**
     * 用于返回验证码字符串
     * @return
     */
    public String getVCode() {
        return vCode;
    }

    /**
     * 配置
     */
    private EmailUtil() {
        props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");  // 使用的协议（JavaMail规范要求）
        props.setProperty("mail.smtp.host", myEmailSMTPHost);  // 发件人的邮箱的 SMTP 服务器地址
        props.setProperty("mail.smtp.auth", "true");  // 需要请求认证
        session = Session.getDefaultInstance(props,new Authenticator(){
            public PasswordAuthentication getPasswordAuthentication()
            {
                return new PasswordAuthentication(fromEmail,fromEmailPw); //发件人邮件用户名、授权码
            }
        });
         session.setDebug(true);  // 设置为debug模式, 可以查看详细的发送 log
    }

    /**
     * 发送邮件
     * @param toEmail
     * @throws Exception
     */
    public void sendEmail(String toEmail) throws Exception {
        try {

        MimeMessage message = new MimeMessage(session);

        // Set From: 头部头字段
        message.setFrom(new InternetAddress(fromEmail));

        // Set To: 头部头字段
        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(toEmail));

        // Set Subject: 头字段
        message.setSubject("邮箱验证码:(^_^)","UTF-8");

        // 发送 HTML 消息, 可以插入html标签
            // /**这里就需要用工具生成的验证码**/
        String generatedCode =VCodeUtils.verifyCode(6);   // 假设后台生成的验证码
            vCode=generatedCode;
        /**这里是将html文件的内容进行更新**/
        String emailBody = vericodeHtml.replace(":data=\"123456\"", ":data=\"" + generatedCode + "\"").replace("1EM456", generatedCode); //将发送页面的验证码改为后台生成的验证码

        message.setText(emailBody, "UTF-8", "html");

        // 发送消息
        Transport.send(message);
        System.out.println("Sent message successfully....");
    }catch (MessagingException mex) {
        mex.printStackTrace();

    }
}
public void sendMessage(String toEmail,String onlyId,int userId){
    New target=new New();
    target.setUserId(userId);
    target.setOnlyId(onlyId);
    service.addPushNew(target);
    try {

        MimeMessage message = new MimeMessage(session);

        // Set From: 头部头字段
        message.setFrom(new InternetAddress(fromEmail));

        // Set To: 头部头字段
        message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(toEmail));

        // Set Subject: 头字段
        message.setSubject("您的快递滞留时间已超过18小时","UTF-8");

        // 发送 HTML 消息, 可以插入html标签
        // /**这里就需要用工具生成的验证码**/
        /**这里是将html文件的内容进行更新**/
        String emailBody = overTimeEmail.replace(":data=\"123456\"", ":data=\"" + onlyId + "\"").replace("1EM456", onlyId); //将发送页面的验证码改为后台生成的验证码

        message.setText(emailBody, "UTF-8", "html");

        // 发送消息
        Transport.send(message);
        System.out.println("Sent message successfully....");



    }catch (MessagingException mex) {
        mex.printStackTrace();

    }

}
}

